package com.duoketech.utils.validator.rules.string;

import com.duoketech.exception.ValidateException;
import com.duoketech.utils.validator.AbstractRule;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringMustBePassWord extends AbstractRule {
    private String rex = "^[a-zA-Z0-9]{6,16}$";//包含大小写字母及数字且在6-16位

    public StringMustBePassWord(String v, String msg) {
        this.value = v;
        this.msg = msg;
    }

    @Override
    public boolean validate() throws ValidateException {
        String v = StringUtils.trim((String) this.value);
        Pattern pattern = Pattern.compile(this.rex);
        Matcher matcher = pattern.matcher(v);
        if (!matcher.matches()) {
            throw new ValidateException(this.msg);
        }
        return true;
    }

    public static void main(String[] args) {

        try {
            new StringMustBePassWord("123acb","包含大小写字母及数字且在6-12位").validate();
        } catch (ValidateException e) {
            e.printStackTrace();
        }
    }
}
